package v1

import (
	"net/http"

	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"

	"server/global/e"
	"server/global/form"
	"server/global/response"
	"server/model"
	"server/service"
)

// @Summary 获取智能场景
// @Produce  json
// @Param id path int true "ID"
// @Success 200 {object} response.Response
// @Failure 500 {object} response.Response
// @Router /api/v1/auto/{id} [get]
func GetAuto(c *gin.Context) {
	r := response.Gin{C: c}
	id := com.StrTo(c.Param("id")).MustInt()
	valid := validation.Validation{}
	valid.Min(id, 1, "id")

	if valid.HasErrors() {
		form.MarkErrors(valid.Errors)
		r.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}

	auto := &model.Auto{ID: id}
	autoInfo, err := service.AutoGet(*auto)
	if err != nil {
		r.Response(http.StatusInternalServerError, e.ERROR_DEVICE_NOT_EXIST, nil)
		return
	}

	r.Response(http.StatusOK, e.SUCCESS, autoInfo)
}

// @Summary 获取所有智能场景
// @Produce  json
// @Param status body int false "Status"
// @Success 200 {object} response.Response
// @Failure 500 {object} response.Response
// @Router /api/v1/auto [get]
func GetAutos(c *gin.Context) {
	r := response.Gin{C: c}
	autos, err := service.AutoGetAll()
	if err != nil {
		r.Response(http.StatusInternalServerError, e.ERROR_DEVICE_GET_ALL, nil)
		return
	}
	r.Response(http.StatusOK, e.SUCCESS, autos)
}

type AddAutoForm struct {
	Name      string `form:"name" valid:"MaxSize(100)"`
	Logic     string `form:"logic" valid:"MaxSize(10)"`
	Condition string `form:"condition" valid:"MaxSize(1000)"`
	Run       string `form:"run" valid:"MaxSize(1000)"`
	Enable    int    `form:"enable" valid:"Min(0)"`
}

// @Summary 添加智能场景
// @Produce  json
// @Param id path int true "ID"
// @Param name query string true "Name"
// @Param position query string true "Position"
// @Success 200 {object} response.Response
// @Failure 500 {object} response.Response
// @Router /api/v1/auto/{id} [PUT]
func AddAuto(c *gin.Context) {
	var (
		r = response.Gin{C: c}
		f AddAutoForm
	)

	httpCode, errCode := form.BindAndValid(c, &f)
	if errCode != e.SUCCESS {
		r.Response(httpCode, errCode, nil)
		return
	}

	auto := &model.Auto{
		Name:      f.Name,
		Logic:     f.Logic,
		Condition: f.Condition,
		Run:       f.Run,
		Enable:    f.Enable,
	}

	// 添加智能场景
	err := service.AutoAdd(*auto)
	if err != nil {
		r.Response(http.StatusOK, e.ERROR_DEVICE_ADD, nil)
		return
	}

	r.Response(http.StatusOK, e.SUCCESS, nil)
}

type EditAutoForm struct {
	ID        int    `form:"id" valid:"Min(1)"`
	Name      string `form:"name" valid:"MaxSize(100)"`
	Logic     string `form:"logic" valid:"MaxSize(10)"`
	Condition string `form:"condition" valid:"MaxSize(1000)"`
	Run       string `form:"run" valid:"MaxSize(1000)"`
	Enable    int    `form:"enable" valid:"Min(0)"`
}

// @Summary 修改智能场景
// @Produce  json
// @Param id path int true "ID"
// @Param name query string true "Name"
// @Param position query string true "Position"
// @Success 200 {object} response.Response
// @Failure 500 {object} response.Response
// @Router /api/v1/auto/{id} [PUT]
func EditAuto(c *gin.Context) {
	var (
		r = response.Gin{C: c}
		f = EditAutoForm{ID: com.StrTo(c.Param("id")).MustInt()}
	)

	httpCode, errCode := form.BindAndValid(c, &f)
	if errCode != e.SUCCESS {
		r.Response(httpCode, errCode, nil)
		return
	}

	// 修改智能场景
	auto := &model.Auto{
		ID:        f.ID,
		Name:      f.Name,
		Logic:     f.Logic,
		Condition: f.Condition,
		Run:       f.Run,
		Enable:    f.Enable,
	}

	_, err := service.AutoExist(*auto)
	if err != nil {
		r.Response(http.StatusInternalServerError, e.ERROR_DEVICE_CHECK_EXIST, nil)
		return
	}

	err = service.AutoEdit(*auto)
	if err != nil {
		r.Response(http.StatusInternalServerError, e.ERROR_DEVICE_EDIT, nil)
		return
	}

	r.Response(http.StatusOK, e.SUCCESS, nil)
}

// @Summary  删除智能场景
// @Produce  json
// @Param id path int true "ID"
// @Success 200 {object} response.Response
// @Failure 500 {object} response.Response
// @Router /api/v1/auto/{id} [DELETE]
func DeleteAuto(c *gin.Context) {
	r := response.Gin{C: c}
	id := com.StrTo(c.Param("id")).MustInt()
	valid := validation.Validation{}
	valid.Min(id, 1, "id")

	if valid.HasErrors() {
		form.MarkErrors(valid.Errors)
		r.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}
	auto := &model.Auto{ID: id}
	exist, err := service.AutoExist(*auto)
	if err != nil {
		r.Response(http.StatusInternalServerError, e.ERROR_DEVICE_CHECK_EXIST, nil)
		return
	}
	if !exist {
		r.Response(http.StatusOK, e.ERROR_DEVICE_NOT_EXIST, nil)
		return
	}
	// 删除智能场景
	err = service.AutoDelete(*auto)
	if err != nil {
		r.Response(http.StatusInternalServerError, e.ERROR_DEVICE_DELETE, nil)
		return
	}

	r.Response(http.StatusOK, e.SUCCESS, nil)
}
